<div class="box">
	<div class="box-header">
		<h3 class="box-title"><i class="fa fa-home"></i> <?=$this->lang->line("ACCOMODATIONS")?> <?=$this->lang->line("YOU_CAN_EDIT")?></h3>
	</div>
	<div class="box-body ">

		<? //Common::pr($accomodationList); ?>

		<table id="accomodationTable" class="display table table-bordered">
			<thead>
				<tr>
					<th class="text-right w1">#</th>
					<th><?=$this->lang->line("ACCOMODATION")?></th>
					<th><?=$this->lang->line("INSTITUTE")?></th>
					<th><?=$this->lang->line("CITY")?></th>
					<th class="w1 ws"><?=$this->lang->line("OPTIONS")?></th>
				</tr>
			</thead>
			<tbody>
				<?php
				foreach($accomodationList as $acc) {
					$rowClass = "";
					
					if($acc["e2cTempFlag"] == "I") {
						$rowClass = "warning";
					} elseif($acc["accPresenter"] == "I") {
						$rowClass = "success";
					}
					
					echo		"<tr class=\"".$rowClass."\">";
					echo			"<td class=\"vm w1 text-right\"></td>";
					echo			"<td class=\"vm\">".$acc["accName"]."</td>";
					echo			"<td class=\"vm\">".$acc["insNameList"]."</td>";
					echo			"<td class=\"vm\">".$acc["cityName"]."</td>";
					echo			"<td class=\"vm w1 ws\">";
					echo				"<button class=\"btn btn-xs btn-primary\" data-acc=\"".$acc["accId"]."\" data-entity=\"ACC\" title=\"".$this->lang->line("EDIT")."\" ><span class=\"glyphicon glyphicon-edit\"></span></button>";
					echo				"&nbsp;<button class=\"btn btn-xs btn-primary ".(!$acc["e2cTempFlag"] ? "disabled" : "")."\" ".($acc["e2cTempFlag"] ? "data-acc-publication=\"".$acc["accId"]."\" data-publication=\"1\" data-entity=\"ACC\"" : "")." title=\"".$this->lang->line("PUBLICATION")."\"><span class=\"glyphicon glyphicon-thumbs-up\"></span></button>";
					echo				"&nbsp;<button class=\"btn btn-xs btn-primary ".(!$acc["e2cTempFlag"] ? "disabled" : "")."\" ".($acc["e2cTempFlag"] ? "data-acc-publication=\"".$acc["accId"]."\" data-publication=\"0\" data-entity=\"ACC\"" : "")." title=\"".$this->lang->line("CANCELLING_OF_UNPUBLISHED_DATA")."\"><span class=\"glyphicon glyphicon-thumbs-down\"></span></button>";
					echo				"&nbsp;<button class=\"btn btn-xs btn-primary ".($acc["accPresenter"] == "I" ? "" : "disabled")."\" ".($acc["accPresenter"] == "I" ? "data-acc-publication=\"".$acc["accId"]."\" data-publication=\"2\" data-entity=\"ACC\"" : "")." title=\"".$this->lang->line("CANCELLING_OF_PUBLISHED_DATA")."\"><span class=\"glyphicon glyphicon-hand-left\"></span></button>";
					echo				"&nbsp;<a class=\"btn btn-xs btn-primary\" href=\"".site_url("preview/accomodations/".$acc["accUrlName"])."\" onclick=\"window.open(this.href); return false;\" title=\"".$this->lang->line("PREVIEW")."\"><span class=\"glyphicon glyphicon-share-alt\"></span></a>";
					echo				"&nbsp;<button class=\"btn btn-xs btn-primary\" data-acc-image=\"".$acc["accId"]."\" data-entity=\"ACC\" title=\"".$this->lang->line("MANAGING_IMAGES")."\"><span class=\"glyphicon glyphicon-picture\"></span></button>";
					echo				"&nbsp;<button class=\"btn btn-xs btn-danger\" data-acc-delete=\"".$acc["accId"]."\" data-entity=\"ACC\" title=\"".$this->lang->line("DELETE")."\"><span class=\"glyphicon glyphicon-remove-circle\"></span></button>";
					echo			"</td>";
					echo		"</tr>";
				}
				?>
			</tbody>
		</table>

		<script>
			$(document).ready( function () {

				var accomodationTable = $('#accomodationTable').DataTable({
					"language": {
						"url": "<?=MEDIA?>admin_lte/DataTables-1.10.3/localization/en.lang"
					},
					"autoWidth": false,
					"columnDefs": [{
						"searchable": false,
						"orderable": false,
						"targets": 0
					}, {
						"searchable": false,
						"orderable": false,
						"targets": -1
					}],
					"order": [[ 1, 'asc' ]],
					stateSave: true
				});

				accomodationTable.on('draw.dt', function () {
					accomodationTable.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
						$(cell).addClass('text-right');
						cell.innerHTML = i+1 + '.';
					});
					eventRebind();
					$('#accomodationTable').removeClass('hidden');
				});

				accomodationTable.on( 'init.dt', function () {
					$('#accomodationTable_length').append('<button class="btn btn-sm btn-success" style="margin-left: 20px;" data-acc="-1" data-entity="ACC"><span class="glyphicon glyphicon-plus"></span> <?=$this->lang->line("ADD_NEW")?></button>');
					eventRebind();
				});

			});

			var aktImageE;

			function imageDisplayRefresh(e) {
				if(!e)
					e = aktImageE;
				$.ajax({
					type: "POST",
					cache: false,
					data: { ENTITY: e.data("entity"), ID: e.data("accImage") },
					url: '<?=site_url("admin_lte/get_ent2image")?>',
					beforeSend: function() {
						$('#studyDisplayModal .modal-title').html('<?=$this->lang->line("MANAGING_IMAGES")?>');
						$('#studyDisplayModal .modal-body form').html('<?=$this->db->escape_str($this->lang->line("PLEASE_WAIT,_PROCESSING_IN_PROGRESS"))?>');
						$('#studyDisplayModal').modal('show');
					},
					success: function (result) {
						$('#studyDisplayModal .modal-body').html(result);
					},
					error: function (error) {}
				});
			}

			function eventRebind() {
				$('button[data-acc]').unbind('click').on('click', function(e) {
					e.preventDefault();
					$this = $(this);
					$.ajax({
						type: "POST",
						cache: false,
						data: { ENTITY: $this.data("entity"), ID: $this.data("acc") },
						url: '<?=site_url("admin_lte/entity_edit")?>',
						beforeSend: function() {
							$('#studyEditorModal .modal-title').html(($this.data('acc') == '-1' ? '<?=$this->db->escape_str($this->lang->line("ADD_NEW"))?>' : '<?=$this->lang->line("EDIT")?>') + ' ' + '<?=strtolower($this->lang->line("ACCOMODATION"))?>');
							$('#studyEditorModal .modal-body form').html('<?=$this->db->escape_str($this->lang->line("PLEASE_WAIT,_PROCESSING_IN_PROGRESS"))?>');
							$('#studyEditorModal').modal('toggle');
						},
						success: function (result) {
							$('#studyEditorModal button.btn-success').data('url', '<?=site_url("admin_lte/entity_insert_update")?>');
							$('#studyEditorModal .modal-body form').html(result);
						},
						error: function (error) {}
					});
				});

				$('button[data-acc-delete]').unbind('click').on('click', function() {
					$this = $(this);
					$('#studyDeleteModal .modal-title').html('<?=$this->lang->line("DELETE")?>');
					$('#studyDeleteModal .modal-body form').html('<?=$this->db->escape_str($this->lang->line("ARE_YOU_SURE_YOU_WOULD_LIKE_TO_DELETE_THE_ITEM"))?>');
					$('#studyDeleteModal .modal-body form').append('<input type="hidden" name="ENTITY" value="ACC" /><input type="hidden" name="del" value="true" /><input type="hidden" name="ID" value="' + $this.data('accDelete') + '" />');
					$('#studyDeleteModal button.btn-danger').data('url', '<?=site_url("admin_lte/entity_insert_update");?>');
					$('#studyDeleteModal').modal('toggle');
				});

				$('button[data-acc-publication]').unbind('click').on('click', function() {
					$this = $(this);
					$('#studyConfirmModal .modal-title').html('<?=$this->lang->line("CONFIRMATION")?>');
					var bodyFormHtml = '';
					switch($this.data('publication')) {
						case 0: { bodyFormHtml = '<?=$this->db->escape_str($this->lang->line("ARE_YOU_SURE_YOU_WOULD_LIKE_TO_CANCEL_THE_UNPUBLISHED_DATA"))?>'; } break;
						case 1: { bodyFormHtml = '<?=$this->db->escape_str($this->lang->line("ARE_YOU_SURE_YOU_WOULD_LIKE_TO_PUBLISH_THE_ITEMS"))?>'; } break;
						case 2: { bodyFormHtml = '<?=$this->db->escape_str($this->lang->line("ALL_UNPUBLISHED_DATA_WILL_BE_ERASED_ARE_YOU_SURE_TO_CONTINUE"))?>'; } break;
					}
					$('#studyConfirmModal .modal-body form').html(bodyFormHtml);
					$('#studyConfirmModal .modal-body form').append('<input type="hidden" name="ENTITY" value="ACC" /><input type="hidden" name="publish" value="' + $this.data('publication') + '" /><input type="hidden" name="ID" value="' + $this.data('accPublication') + '" />');
					$('#studyConfirmModal button.btn-success').data('url', '<?=site_url("admin_lte/entity_insert_update");?>');
					$('#studyConfirmModal').modal('toggle');
				});

				$('button[data-acc-image]').unbind('click').on('click', function(e) {
					e.preventDefault();
					$this = $(this);
					aktImageE = $this;
					imageDisplayRefresh($this);
				});

			}

		</script>

	</div>
</div>